package guda.sqladmin.biz.impl;

import java.util.Date;
import java.util.List;

import guda.sqladmin.biz.query.TableQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.sqladmin.biz.ProjectTableBiz;
import guda.sqladmin.dao.ProjectTableDOMapper;
import guda.sqladmin.dao.domain.ProjectTableDO;
import guda.sqladmin.dao.domain.ProjectTableDOCriteria;
import guda.tools.web.page.BaseQuery;
import guda.tools.web.page.BizResult;

public class ProjectTableBizImpl implements ProjectTableBiz{

    private final static Logger logger = LoggerFactory.getLogger(ProjectTableBizImpl.class);

    @Autowired
    private ProjectTableDOMapper projectTableDOMapper;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            ProjectTableDO projectTableDO = projectTableDOMapper.selectByPrimaryKey(id);
            bizResult.data.put("projectTableDO", projectTableDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query ProjectTable error",e);
        }
        return bizResult;
}

    public BizResult list(TableQuery tableQuery) {
        BizResult bizResult = new BizResult();
        try {
            ProjectTableDOCriteria projectTableDOCriteria = new ProjectTableDOCriteria();
            projectTableDOCriteria.setStartRow(tableQuery.getStartRow());
            projectTableDOCriteria.setPageSize(tableQuery.getPageSize());
            projectTableDOCriteria.createCriteria().andProjectIdEqualTo(tableQuery.getProjectId());
            int totalCount = projectTableDOMapper.countByExample(projectTableDOCriteria);
            tableQuery.setTotalCount(totalCount);
            List<ProjectTableDO> projectTableDOList = projectTableDOMapper.selectByExample(projectTableDOCriteria);
            bizResult.data.put("projectTableDOList", projectTableDOList);
            bizResult.data.put("query", tableQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view ProjectTable list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            projectTableDOMapper.deleteByPrimaryKey(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete projectTable error", e);
        }
        return bizResult;
    }

    public BizResult create(ProjectTableDO projectTableDO) {
        BizResult bizResult = new BizResult();
        try {
            projectTableDO.setIsDeleted(0);
            projectTableDO.setGmtCreate(new Date());

            int count = projectTableDOMapper.insert(projectTableDO);
            bizResult.data.put("count", count);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create ProjectTable error", e);
        }
        return bizResult;
    }

    public BizResult update(ProjectTableDO projectTableDO) {
        BizResult bizResult = new BizResult();
        try {
            int count = projectTableDOMapper.updateByPrimaryKeySelective(projectTableDO);
            bizResult.data.put("count", count);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update ProjectTable error", e);
        }
        return bizResult;
    }

    }
